package com.geostar.constructionland.statistics.service.handler;

import com.geostar.constructionland.statistics.enumeration.RequestType;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

/***
 * @Author makoto_zhu
 * @Date 2020-10-29 03:55:42
 * @Description 数据精度处理
 **/
@Component
@Scope("prototype")
public class DataProcessHandler extends RequestHandler {

    @Override
    public void handleRequest(Request req) {
        if (RequestType.PERCISION_PROCESS == req.getRequestType()) {
            Map<String, Object> data = (Map<String, Object>) req.getData();
            Integer percision = (Integer) data.get("percision");
            List<Map<String, Object>> resultList = (List<Map<String, Object>>) data.get("resultList");


            percision = percision == null ? 0 : percision;
            for (Map<String, Object> map : resultList) {
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    Object value = entry.getValue();
                    if (value instanceof Integer || value instanceof BigDecimal || value instanceof Long) {
                        value = new BigDecimal(String.valueOf(value)).setScale(percision, RoundingMode.HALF_UP).toString();
                    }
                    map.put(entry.getKey(), value);
                }
            }
        }
    }
}
